using UnityEngine;
using System.Collections;

public class test1 : MonoBehaviour {
	
	private int fucku;
	public float rotate_speed;
	public Transform ob;
	Quaternion final_rotation;
	bool rotating_change_target;
	bool is_rotating;
	// Use this for initialization
	void Start () {
		Reflect_LR();
		Reflect_LR();
	//StartCoroutine(asd());
			// rotating_change_target = false;
	// is_rotating = false;
	//ob = GameObject.Find("focus").transform;
		//rotate_speed = 45;
	}
	
	// Update is called once per frame
	void Update () {
	//Rotate_XAxis_To(ob.transform.position);
	}
	
	IEnumerator asd()
	{
		yield return 10.0;
		print("suck");
	}
	protected void Reflect_LR()
	{
		
		//flip_y = -flip_y;
		Mesh mesh = this.GetComponent<MeshFilter>().mesh;	
		int i = 0;
		int vertex_cnt = mesh.vertices.Length;
		int[] new_tri = new int[6];
		//更改顶点绘制顺序（翻转表面）
	
			new_tri[0] = 0;
			new_tri[1] = mesh.triangles[2];
			new_tri[2] = mesh.triangles[1];
			
			new_tri[3] = mesh.triangles[4];
			new_tri[4] = mesh.triangles[3];
			new_tri[5] = 1;

		mesh.triangles = new_tri;
		transform.Rotate(Vector3.up * 180 , Space.World);
		/*Debug.Log(mesh.triangles[0] );
			Debug.Log(mesh.triangles[1] );
			Debug.Log(mesh.triangles[2] );
			Debug.Log(mesh.triangles[3] );
			Debug.Log(mesh.triangles[4] );
			Debug.Log(mesh.triangles[5] );*/
	}
	virtual protected void a()
	{
			
	}
	
	
		//让物体X轴对准target_position
		public void Rotate_XAxis_To(Vector3 target_position)
		{	
			//if we put the next 3 lines in coroutine,we need to restart coroutine when is_rotating(n=0 n=1);
			Vector3 dir = target_position - transform.position;
			//只在x-y平面旋转
			dir.z = 0;
			final_rotation = Quaternion.LookRotation(new Vector3(0,0,1) , Vector3.Cross (new Vector3(0,0,1) , dir));
			if (is_rotating)
			{
				rotating_change_target = true;
			}
			else
			{
				is_rotating = true;
				StartCoroutine(Rotate_Coroutine());
			}
   			
		}
		
		private IEnumerator Rotate_Coroutine()
		{
			float total_angle = 0;
			float angle_pass = 0;
			//设置新旋转方向
			while(true)
			{
				total_angle = Quaternion.Angle(transform.rotation , final_rotation);
				angle_pass = 0;
				while(true)
				{
					if (rotating_change_target)
					{
						rotating_change_target = false;
						break;
					}
					//逐渐将X轴转向对准目标的位置
					if (angle_pass < total_angle)
					{
						angle_pass += rotate_speed*Time.deltaTime;
						transform.rotation = Quaternion.Slerp (transform.rotation, final_rotation, angle_pass / total_angle);
					}
					//旋转完成(rotating finish)********************************
					else
					{
						is_rotating = false;
					
						yield break;	
					}
					
					yield return null;
					
				}
			}
				
		}
}
